package com.pk.platform.business.charge.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.charge.service.IChargeIndexService;
import com.pk.platform.business.charge.service.IRefundService;
import com.pk.platform.business.charge.service.ITuitionDetailService;
import com.pk.platform.business.charge.service.ITuitionService;
import com.pk.platform.business.charge.vo.RefundVO;
import com.pk.platform.business.charge.vo.TuitionDetailVO;
import com.pk.platform.business.common.service.IChildService;
import com.pk.platform.business.common.service.IKindergartenService;
import com.pk.platform.business.core.web.GenericAction;
import com.pk.platform.domain.charge.ChargeIndex;
import com.pk.platform.domain.charge.Refund;
import com.pk.platform.domain.charge.RefundDetail;
import com.pk.platform.domain.charge.TuitionDetail;
import com.pk.platform.domain.common.Child;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.DateConverter;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.PrivilegeConstant;

public class RefundAction extends GenericAction {
	
	private IRefundService refundService;
	
	private Refund refund = new Refund();
	
	private RefundDetail refundDetail = new RefundDetail();
	
	private RefundVO rvo = new RefundVO();
	
	private Child child = new Child();
	
	private IChildService childService;
	
	private IKindergartenService kindergartenService;
	
	private ITuitionService tuitionService;

	private ITuitionDetailService tuitionDetailService;
	
	private TuitionDetailVO tdvo = new TuitionDetailVO();
	
	private Map<String, Object> returnObj = new HashMap<String, Object>();
	
	private String[] rfIds;
	
	private List<ChargeIndex> indexList = new ArrayList<ChargeIndex>();
	
	private IChargeIndexService chargeIndexService;
	
	private TuitionDetail tuitionDetail = new TuitionDetail();
	
	private Double maxRefundAmount;
	
	private User user = new User();
	
	private Kindergarten kindergarten = new Kindergarten();
	
	private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
	
	private int tdWidth;
	
	private int tdColspan;
	
	private double totalAmount;
	
	public String refundManage()
	{
		rvo.setKindergartenId(kindergartenId);
		lp = refundService.queryRefund(rvo, pager);
		operateChn = PrivilegeConstant.getOperate(PrivilegeConstant.UPDATE_REFUND);
		return SUCCESS;
	}
	
	public String refundDetailManage()
	{
		lp = refundService.queryRefundDetail(rvo, pager);
		refund = refundService.queryRefundById(rvo.getRefund().getId());
		return SUCCESS;
	}
	
	public String toAddRefund()
	{
		child = childService.queryChildById(refund.getChild().getId());
		today = DateConverter.getToday();
		user = this.getLoginUser();
		kindergarten = kindergartenService.queryKindergartenById(kindergartenId);
		return SUCCESS;
	}
	
	public String addRefund()
	{
		User loginUser = this.getLoginUser();
		Kindergarten kinder = kindergartenService.queryKindergartenById(kindergartenId);
		refund.setKindergarten(kinder);
		refund.setRecordUser(loginUser);
		refundService.addRefund(refund);
		returnObj.put("id", refund.getId());
		return SUCCESS;
	}
	
	public String toUpdateRefund()
	{
		refund = refundService.queryRefundById(refund.getId());
		return SUCCESS;
	}
	
	public String updateRefund()
	{
		refundService.updateRefund(refund);
		return SUCCESS;
	}
	
	public String delRefund()
	{
		refundService.delRefund(refund.getId());
		return SUCCESS;
	}
	
	public String tuitionAndDetail()
	{
		indexList = chargeIndexService.queryChargeIndexList();
		lp = refundService.queryTuitionAndDetail(tdvo, pager);
		return SUCCESS;
	}
	
	public String toAddRefundDetail()
	{
		tuitionDetail = tuitionDetailService.queryTuitionDetailById(tuitionDetail.getId());
		return SUCCESS;
	}
	
	public String addRefundDetail()
	{
		refundService.addRefundDetail(refundDetail, maxRefundAmount);
		return SUCCESS;
	}
	
	public String delRefundDetail()
	{
		if(rfIds != null)
		{
			refundService.delRefundDetail(rfIds);
		}
		return SUCCESS;
	}
	
	public String toPrintRefund()
	{
		refund = refundService.queryRefundById(refund.getId());
		list = refundService.queryRefundDetailList(refund.getId());
		int size = list.size() == 0 ? 1 : list.size();
		tdWidth = 100 / size;
		tdColspan = size;
		totalAmount = 0;
		for(Map<String, Object> map : list)
		{
			totalAmount += Double.parseDouble(map.get("amount") == null ? "0" : map.get("amount").toString());
		}
		return SUCCESS;
	}
	
	public String getIntTotalAmount()
	{
		String total = this.getTotalAmount() + "";
		String result = StringConverter.toNBitString(total, 0);
		return result;
	}
	
	
	
	
	
	

	public IRefundService getRefundService() {
		return refundService;
	}

	public void setRefundService(IRefundService refundService) {
		this.refundService = refundService;
	}

	public Refund getRefund() {
		return refund;
	}

	public void setRefund(Refund refund) {
		this.refund = refund;
	}

	public RefundDetail getRefundDetail() {
		return refundDetail;
	}

	public void setRefundDetail(RefundDetail refundDetail) {
		this.refundDetail = refundDetail;
	}

	public RefundVO getRvo() {
		return rvo;
	}

	public void setRvo(RefundVO rvo) {
		this.rvo = rvo;
	}

	public Child getChild() {
		return child;
	}

	public void setChild(Child child) {
		this.child = child;
	}

	public IChildService getChildService() {
		return childService;
	}

	public void setChildService(IChildService childService) {
		this.childService = childService;
	}

	public IKindergartenService getKindergartenService() {
		return kindergartenService;
	}

	public void setKindergartenService(IKindergartenService kindergartenService) {
		this.kindergartenService = kindergartenService;
	}

	public ITuitionService getTuitionService() {
		return tuitionService;
	}

	public void setTuitionService(ITuitionService tuitionService) {
		this.tuitionService = tuitionService;
	}

	public ITuitionDetailService getTuitionDetailService() {
		return tuitionDetailService;
	}

	public void setTuitionDetailService(ITuitionDetailService tuitionDetailService) {
		this.tuitionDetailService = tuitionDetailService;
	}

	public TuitionDetailVO getTdvo() {
		return tdvo;
	}

	public void setTdvo(TuitionDetailVO tdvo) {
		this.tdvo = tdvo;
	}

	public Map<String, Object> getReturnObj() {
		return returnObj;
	}

	public void setReturnObj(Map<String, Object> returnObj) {
		this.returnObj = returnObj;
	}

	public String[] getRfIds() {
		return rfIds;
	}

	public void setRfIds(String[] rfIds) {
		this.rfIds = rfIds;
	}

	public List<ChargeIndex> getIndexList() {
		return indexList;
	}

	public void setIndexList(List<ChargeIndex> indexList) {
		this.indexList = indexList;
	}

	public IChargeIndexService getChargeIndexService() {
		return chargeIndexService;
	}

	public void setChargeIndexService(IChargeIndexService chargeIndexService) {
		this.chargeIndexService = chargeIndexService;
	}

	public TuitionDetail getTuitionDetail() {
		return tuitionDetail;
	}

	public void setTuitionDetail(TuitionDetail tuitionDetail) {
		this.tuitionDetail = tuitionDetail;
	}

	public Double getMaxRefundAmount() {
		return maxRefundAmount;
	}

	public void setMaxRefundAmount(Double maxRefundAmount) {
		this.maxRefundAmount = maxRefundAmount;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Kindergarten getKindergarten() {
		return kindergarten;
	}

	public void setKindergarten(Kindergarten kindergarten) {
		this.kindergarten = kindergarten;
	}

	public List<Map<String, Object>> getList() {
		return list;
	}

	public void setList(List<Map<String, Object>> list) {
		this.list = list;
	}

	public int getTdWidth() {
		return tdWidth;
	}

	public void setTdWidth(int tdWidth) {
		this.tdWidth = tdWidth;
	}

	public int getTdColspan() {
		return tdColspan;
	}

	public void setTdColspan(int tdColspan) {
		this.tdColspan = tdColspan;
	}

	public double getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}

}
